import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import login from '../view/login/login.vue'
import index from '../view/index.vue'
import register from '../view/login/register.vue'
import first from '../view/fistPage/index.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/login'  // 将根路径重定向到登录页
  },
  {
    path: '/login',
    name:'login',
    component:login
  },  // 这里必须加逗号
  {
    path: '/register',
    name:'register',
    component: register
  },
  {
    path: '/home',
    name:'home',
    component: index,
    meta: { requiresAuth: true },  // 建议添加认证元信息
    children:[
      {
        path:'',
        name:'first',
        component:first, // 默认子路由
      },
      {
        path:'product',
        component: () => import('../view/product/product.vue'),
      },
      {
        path:'productadd',
        component: () => import('../view/product/productAdd.vue')
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// // 建议取消注释路由守卫（修改后版本）
// router.beforeEach((to) => {
//   const token = localStorage.getItem('token')
  
//   if (to.meta.requiresAuth && !token) {
//     return '/login'
//   }
  
//   if (to.path === '/login' && token) {
//     return '/home'  // 修改为跳转到/home
//   }
// })

export default router